package com.ruoyi.exam.util;

import java.util.Arrays;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.exam.domain.ExamUserInfo;

public class AiUtil {

      public static JSONObject getAnalyze(ExamUserInfo examUserInfo)
                  throws ApiException, NoApiKeyException, InputRequiredException {
            Generation gen = new Generation();
            Message systemMsg = Message.builder()
                        .role(Role.SYSTEM.getValue())
                        .content("You are a helpful assistant.")
                        .build();
            Message userMsg = Message.builder()
                        .role(Role.USER.getValue())
                        .content(examUserInfo.getAiRequestString())
                        .build();
            GenerationParam param = GenerationParam.builder()
                        .apiKey("sk-828224c476d6466d8e28f40061c80ba6")
                        .model("qwen2-7b-instruct")
                        .messages(Arrays.asList(systemMsg, userMsg))
                        .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                        .build();
            return JSONObject.from(gen.call(param));
      }

      public static String sendAi(String text) throws ApiException, NoApiKeyException, InputRequiredException {
            Generation gen = new Generation();
            Message systemMsg = Message.builder()
                        .role(Role.SYSTEM.getValue())
                        .content("You are a helpful assistant.")
                        .build();
            Message userMsg = Message.builder()
                        .role(Role.USER.getValue())
                        .content(text)
                        .build();
            GenerationParam param = GenerationParam.builder()
                        .apiKey("sk-828224c476d6466d8e28f40061c80ba6")
                        .model("qwen2-7b-instruct")
                        .messages(Arrays.asList(systemMsg, userMsg))
                        .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                        .build();
            JSONObject from = JSONObject.from(gen.call(param));
            String res = "";
            JSONObject output = from.getJSONObject("output");
            if (output == null) {
                  res = "出现错误请重试";
                  return res;
            }
            for (Object item : output.getJSONArray("choices")) {
                  JSONObject message = JSONObject.from(item);
                  res = message.getJSONObject("message").getString("content");
            }
            return res;
      }

}
